package br.ledtec.hardware.fetin2014.model;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketST {
	
	public static ServerSocket serverSocket;
	public static Socket senssionSocket;
	public static ObjectOutputStream saida;
	
	public boolean InitializeSocket(){
		boolean status = false;
		
		short port = 456;						
		
		// Responsavel por atender pedidos via rede na porta determinada
		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println(e);
		} finally{
			System.out.println("Primeiro try");
		}
		
		// Apos receber uma conexao este objeto fica responsavel por manter a comunicacao entre cliente e servidor
		try {
			senssionSocket = serverSocket.accept();
		} catch (IOException e) {
			System.out.println(e);
		}finally{
			System.out.println("Segundo try");
		}
		
		try {
			saida = new ObjectOutputStream(senssionSocket.getOutputStream());
		} catch (IOException e) {
			System.out.println(e);
		}finally{
			System.out.println("Terceiro try");
		}
		
		return status;
	}
	
	public boolean sendIntensityForLumBySocket(String luminaria, String intensidade) {
		boolean status = false;
		
		try {
			saida.flush();
			saida.writeObject("$" + luminaria + intensidade);
//			saida.flush();
//			Thread.sleep(500);
//			saida.writeObject("Liga" + intensidade);
			//saida.flush();
			// saida.reset();
		} catch (IOException e) {
			e.printStackTrace();
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		}finally{
			System.out.println("Quarto try");
		}
		
		return status;
	}
	
//	public boolean sendOffForLum1BySocket() {
//		boolean status = false;
//		
//		try {
//			saida.flush();
//			saida.writeObject("Luminaria1");
//			Thread.sleep(750);
//			saida.writeObject("Desliga");
//			saida.reset();
//		} catch (IOException e) {
//			System.out.println(e);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			System.out.println("Quinto try");
//		}
//		
//		return status;
//	}	
	
//	public boolean sendIntensityForLum2BySocket(String intensidade) {
//		boolean status = false;
//		
//		try {
//			saida.flush();
//			saida.writeObject("Luminaria02");
//			Thread.sleep(500);
//			saida.writeObject("Liga" + intensidade);
//			saida.reset();
//		} catch (IOException e) {
//			System.out.println(e);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			System.out.println("Quarto try");
//		}
//		
//		return status;
//	}
	
//	public boolean sendOffForLum2BySocket() {
//		boolean status = false;
//		
//		try {
//			saida.flush();
//			saida.writeObject("Luminaria02");
//			Thread.sleep(500);
//			saida.writeObject("Desliga");
//			saida.reset();
//		} catch (IOException e) {
//			System.out.println(e);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			System.out.println("Quinto try");
//		}
//		
//		return status;
//	}
	
	public boolean closeSocket() {
		boolean status = false;
		
		try {
			senssionSocket.close();
		} catch (IOException e) {
			System.out.println(e);
		}finally{
			System.out.println("Sexto try");
		}
		
		return status;
	}
}














